package com.leetcode.LC;

public class LC58 {
    public int lengthOfLastWord(String s) {

        int count = 0;
        for (int i = s.length() - 1; i >= 0; i--) {
            if (s.charAt(s.length() - 1) == ' ' && i == s.length() - 1) {
                int k = s.length() - 1;
                while (k>=0&&s.charAt(k) == ' ') {
                    k--;
                }
                i = k;
                if (i<0){
                    return 0;
                }
            }
            if (s.charAt(i) != ' ') {
                count++;
            } else {
                break;
            }
        }
        return count;
    }

    public int lengthOfLastWord2(String s) {

        String[] s1 = s.split(" ");
        if (s1.length==0){
            return 0;
        }
        return s1[s1.length-1].length();
    }

    public static void main(String[] args) {
        System.out.println(new LC58().lengthOfLastWord2("  "));
    }
}
